<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../js/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/common.css" media="all">
    <script src="../../js/layui/layui.all.js"></script>
    <script src="../../js/token.js"></script>
    <script src="../../js/config.js"></script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <ul id="demo1"></ul>
    </div>
    <div style="text-align: center;">
        <button class="layui-btn layuiadmin-btn-admin save">保存</button>
    </div>
</div>

</body>

<script>

    var $ = layui.$ //由于layer弹层依赖jQuery，所以可以直接得到
    var layer = layui.layer
    var tree = layui.tree;
    var Alldata;
    var array=[];
    $.ajax({
       type:'get',
       url:HOST+'admin/permission/findPermissionByRole',
        data:{
           roleId:getUrlSearch("roleId")
        },
        async:false,
        success:function (data) {
           Alldata = data.data.data;
           array = data.data.array;
       }
    });
    function removeByVal(arrylist , val) {
        console.log(val)
        for(var i = 0; i < arrylist .length; i++) {
            if(arrylist [i] == val) {
                arrylist .splice(i, 1);
                break;
            }
        }
    }
    var inst1 = tree.render({
        id:'demo1',
        elem: '#demo1',  //绑定元素
        showCheckbox:true,
        data: Alldata,
        oncheck: function(obj){
            /*if(obj.checked==true){

            }else{

            }*/
        }
    });
    // console.log(array)
    tree.setChecked('demo1',array);

    function getUrlSearch(name) {
        // 未传参，返回空
        if (!name) return null;
        // 查询参数：先通过search取值，如果取不到就通过hash来取
        var after = window.location.search;
        after = after.substr(1) || window.location.hash.split('?')[1];
        // 地址栏URL没有查询参数，返回空
        if (!after) return null;
        // 如果查询参数中没有"name"，返回空
        if (after.indexOf(name) === -1) return null;

        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        // 当地址栏参数存在中文时，需要解码，不然会乱码
        var r = decodeURI(after).match(reg);
        // 如果url中"name"没有值，返回空
        if (!r) return null;

        return r[2];
    }

    //获取所有选中的节点id
    function getCheckedId(data) {
        var id = "";
        $.each(data, function (index, item) {
            if (id != "") {
                id = id + "," + item.id;
            }
            else {
                id = item.id;
            }
            //item 没有children属性
            if (item.children != null) {
                var i = getCheckedId(item.children);
                if (i != "") {
                    id = id + "," + i;
                }
            }
        });
        return id;
    }

    $(".save").on('click',function () {
        var menus = getCheckedId(tree.getChecked('demo1'));
        //console.log(menus);
        $.ajax({
            type: "POST",
            url: HOST+"admin/permission/saveOrDelPermissionByRole",
            traditional:true,
            data:{
                roleId:getUrlSearch('roleId'),
                permissionIdArray:menus
            },
            dataType: "json",
            async:false,
            success:function (data) {
                if(data.code==200){
                layer.msg('保存成功',{
                    icon:1,
                    time:1000,
                    end:function () {
                        window.parent.location.reload();
                    }
                })
                }else{
                    layer.msg('保存失败',{
                        icon:2,
                        time:1000,
                        end:function () {
                            window.parent.location.reload();
                        }
                    })
                }
            }
        });
    })

</script>
</html>
